package com.practice.LocalDateTime和ZonedDateTime等java8中全新的时间API相关操作;

import java.time.*;

/**
 * @program: tenghw-practice-demo
 * @author: tenghw
 * @create: 2024-01-15 19:57
 * @Version 1.0
 * @description:
 **/
public class Demo4 {
    public static void main1(String[] args) {
        //1.如果时间是时间戳的方式，获取两个时间间隔
        Instant instant1 = Instant.now();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant instant2 = Instant.now();
        System.out.println(instant1);
        System.out.println(instant2);
        //比较之后得到的是一个Duration对象，然后调用对象相应的方法
        Duration duration = Duration.between(instant1, instant2);
        System.out.println(duration.getSeconds());
        System.out.println(duration.toMillis());
        System.out.println(duration.getNano());

        // 2.如果时间是时间方式，获取两个时间间隔
        LocalTime localTime1 = LocalTime.of(12, 4, 33);
        LocalTime localTime2 = LocalTime.of(12, 5, 45);
        Duration duration1 = Duration.between(localTime1, localTime2);
        System.out.println(duration1.getSeconds());// 72
        System.out.println(duration1.toMinutes());// 1
    }

    public static void main(String[] args) {
        LocalDate localDate1 = LocalDate.of(2018, 4, 14);
        //LocalDate localDate2 = LocalDate.now();
        LocalDate localDate2 = LocalDate.of(2024, 10, 10);
        Period period=Period.between(localDate1, localDate2);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }

}